!function () {
 var Radar = Object.create(Object);
 
// Object.defineProperties(Stack, {
//					"CreateNew": {
//						"value": function() {
//							return FCreateNew();
//						}
//					}
//				});
 Object.defineProperties(Radar,{
 	"CreateNew":{
 		"value":function () {
 		    return FCreateNew();
 		}
 	}
 });
 function FCreateNew() {
 	var radar = Object.create(Radar);
 	radar = {
 		"ValueToProperty":function (v,name) {
 		    this[name] = v;
 		    return this;
 		},
 		"_labelid":undefined,
 		"_width":undefined,
 		"_height":undefined,
 		"_node_radius":undefined,
 		"_data":undefined,
 		"_colorScale":undefined,
 		"_factor":undefined,
 		"_factorLegend":undefined,
 		"_level":undefined,
 		"_opacityArea":undefined,
 		"_toRight":undefined,
 		"_translateX":undefined,
 		"_translateY":undefined,
 		"_extraWidthX":undefined,
 		"_extraWidthY":undefined
 	};
 	Object.defineProperties(radar,{
 		"Labelid":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_labelid");
 			    return this;
 			}
 		},
 		"Width":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_width");
 			    return this;	
 			}
 		},
 		"Height":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_height");
 			    return this;
 			}
 		},
 		"Node_radius":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_node_radius");
 			    return this;
 			}
 		},
 		"Data":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_data");
 			    return this;
 			}
 		},
 		"ColorScale":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_colorScale");
 			    return this;
 			}
 		},
 		"Factor":{
 			"value":function (v) {
 				this.ValueToProperty(v,"_factor");
 			    return this;
 			}
 		},
 		"FactorLegend":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_factorLegend");
 			    return this;
 			}
 		},
 		"Level":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_level");
 			    return this;
 			}
 		},
 		"OpacityArea":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_opacityArea");
 			    return this;
 			}
 		},
 		"ToRight":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_toRight");
 			    return this;
 			}
 		},
 		"TranslateX":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_translateX");
 			    return this;
 			}
 		},
 		"TranslateY":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_translateY");
 			}
 		},
 		"ExtraWidthX":{
 			"value":function (v) {
 			    this.ValueToProperty(v,"_extraWidthX");
 			    return this;
 			}
 		},
 		"ExtraWidthY":{
 			"value":function (v) {
 				this.ValueToProperty(v,"_extraWidthY");
 			    return this;
 			}
 		},
 		"CreateSvg":{
 			"value":function () {
 			    DetermineProperties(this);
 			    return this;
 			}
 		},
 		"Draw":{
 			"value":function () {
 			    Detect_function(this._data) ?
				alert("未指定data") || console.log("未指定data") :
				DataPretreatment_Radar(this) ||
				Tool_Radar(this) ||
				Draw_Radar(this);
			return this;
 			}
 		}
 	});
 	function DataPretreatment_Radar(a) {
			console.log(a._data.data);
			a._maxValue = Rounding(d3.max(a._data.data, function(d) {
				console.log(d);
				return d3.max(d.value, function(i) {
					return i[1];
				})
			}));
			a._allAxis = a._data.data[0].value.map(function(d) {
				return d[0];
			})
			a._total = a._allAxis.length;
			a._radius = (a._width - a._ExtraWidthX) / 2;

		}

		function Tool_Radar(a) {
			a.Format = d3.format('');
		}

		function DetermineProperties(a) {
//			Detect_function(a._labelid) ?
//				console.log("请添加svg父标签id用于添加svg") :
//				d3.select("#" + a._labelid)[0][0] == null ?
//				console.log("请检查id是否正确") :
				SetId(a);
			a._width = Detect_function(a._width) ? (d3.select("#" + a._labelid))[0][0].clientWidth : a._width;
			a._height = Detect_function(a._height) ? a._width : a._height;
			a._ExtraWidthX = Detect_function(a._ExtraWidthX) ? 100 : a._ExtraWidthX;
			a._ExtraWidthY = Detect_function(a._ExtraWidthY) ? 100 : a._ExtraWidthY;
			a._radarWidth = a._width - a._ExtraWidthX * 2;
			a._factor = Detect_function(a._factor) ? 1 : a._factor;
			a._factorLegend = Detect_function(a._factorLegend) ? 0.85 : a._factorLegend;
			a._levels = Detect_function(a._levels) ? 5 : a._levels;
			a._radians = 2 * Math.PI;
			a._opacityArea = Detect_function(a._opacityArea) ? 0.5 : a._opacityArea;
			a._ToRight = Detect_function(a._ToRight) ? 5 : a._ToRight;
			a._TranslateX = Detect_function(a._TranslateX) ? 0 : a._TranslateX;
			a._TranslateY = Detect_function(a._TranslateY) ? 0 : a._TranslateY;
			a._colorScale = Detect_function(a._colorScale) ? d3.scale.category10() : a._colorScale;
			a._node_radius = Detect_function(a._node_radius) ? 5 : a._node_radius;
			CreateSvg_Radar(a);
		}

		function SetId(a) {
			//svg
			a._svgid = a._labelid + "Svg";
			//radar
			a._svg_radar_g_id = a._labelid + "Radar_g";
			//radar_bg
			a._svg_radar_g_bgline_g_id = a._labelid + "Radar_g_bgline_g";
			//radar_bg_
			a._svg_radar_g_bgline_class = a._labelid + "Radar_g_bgline_class"
			a._svg_radar_g_bgtext_g_id = a._labelid + "Radar_g_bgtext_g";
			//radar_bg_
			a._svg_radar_g_bgtext_class = a._labelid + "Radar_g_bgtext_class"
				//radar_axes
			a._svg_radar_g_axes_g_id = a._labelid + "Radar_g_axes_g";
			a._svg_radar_g_axes_line_class = a._labelid + "Radar_g_axes_line_class";
			a._svg_radar_g_axes_text_class = a._labelid + "Radar_g_axes_text_class";
			//radar_area||node||tooltip
			a._svg_radar_g_area_g_id = a._labelid + "Radar_g_area_g";
			a._svg_radar_g_area_class = a._labelid + "Radar_g_area_class";
			a._svg_radar_g_node_g_id = a._labelid + "Radar_g_node_g";
			a._svg_radar_g_node_class = a._labelid + "Radar_g_node_class";
			a._svg_radar_g_Tooltip_g_id = a._labelid + "Radar_g_Tooltip_g";
			a._svg_radar_g_Tooltip_class = a._labelid + "Radar_g_Tooltip_class";
			//legend
			a._svg_legend_g_id = a._labelid + "Legend_g";
			//legend_title
			a._svg_legend_g_title_g_id = a._labelid + "Legend_title_g";
			a._svg_legend_g_title_class = a._labelid + "Legend_title_class";
			//legend_rect
			a._svg_legend_g_rect_g_id = a._labelid + "Legend_rect_g";
			a._svg_legend_g_rect_class = a._labelid + "Legend_rect_class";
			//legend_text
			a._svg_legend_g_text_g_id = a._labelid + "Legend_text_g";
			a._svg_legend_g_text_class = a._labelid + "Legend_text_class";

		}

		function Draw_Radar(a) {
			//  ========== 
			//  = 背景 = 
			//  ========== 
			//line
			a.svg.remove();
			CreateSvg_Radar(a);
			for(var j = 0; j < a._levels; j++) {
				a._levelFactor = a._factor * a._radius * ((j + 1) / a._levels);
				a.radar_bgline_updata = a.radar_bgline_g.selectAll(".levels")
					.data(a._allAxis);
				a.radar_bgline_enter = a.radar_bgline_updata.enter();
				a.radar_bgline_exit = a.radar_bgline_updata.exit();
				a.radar_bgline_updata
					.attr("x1", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.sin(i * a._radians / a._total));
					})
					.attr("y1", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.cos(i * a._radians / a._total));
					})
					.attr("x2", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.sin((i + 1) * a._radians / a._total));
					})
					.attr("y2", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.cos((i + 1) * a._radians / a._total));
					})
					.attr("class", a._svg_radar_g_bgline_class + j)
					.style("stroke", "#bbb")
					.style("stroke-opacity", "0.75")
					.style("stroke-width", "1px")
					.attr("transform", "translate(" + (a._width / 2 - a._levelFactor) + ", " + (a._height / 2 - a._levelFactor) + ")");
				a.radar_bgline_enter
					.append("line")
					.attr("x1", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.sin(i * a._radians / a._total));
					})
					.attr("y1", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.cos(i * a._radians / a._total));
					})
					.attr("x2", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.sin((i + 1) * a._radians / a._total));
					})
					.attr("y2", function(d, i) {
						return a._levelFactor * (1 - a._factor * Math.cos((i + 1) * a._radians / a._total));
					})
					.attr("class", a._svg_radar_g_bgline_class + j)
					.style("stroke", "#bbb")
					.style("stroke-opacity", "0.75")
					.style("stroke-width", "1px")
					.attr("transform", "translate(" + (a._width / 2 - a._levelFactor) + ", " + (a._height / 2 - a._levelFactor) + ")");
				a.radar_bgline_exit.remove();
				//text
				a.radar_bgtext_g.selectAll(".levels")
					.data([1]) //dummy data
					.enter()
					.append("text")
					.attr("x", function(d) {
						return a._levelFactor * (1 - a._factor * Math.sin(0));
					})
					.attr("y", function(d) {
						return a._levelFactor * (1 - a._factor * Math.cos(0));
					})
					.attr("class", a._svg_radar_g_bgtext_class + j)
					.style("font-family", "sans-serif")
					.style("font-size", "10px")
					.attr("transform", "translate(" + (a._width / 2 - a._levelFactor + a._ToRight) + ", " + (a._height / 2 - a._levelFactor) + ")")
//					.attr("fill", "#737373")
					.text(a.Format((j + 1) * a._maxValue / a._levels));
			}
			//text说明

			a.radar_axes_line = a.radar_axes_g.selectAll(".axis")
				.data(a._allAxis)
				.enter()
				.append("line")
				.attr("x1", a._width / 2)
				.attr("y1", a._height / 2)
				.attr("x2", function(d, i) {
					return a._levelFactor * (1 - a._factor * Math.sin(i * a._radians / a._total)) + a._ExtraWidthX / 2;
				})
				.attr("y2", function(d, i) {
					return a._levelFactor * (1 - a._factor * Math.cos(i * a._radians / a._total)) + a._ExtraWidthY / 2;
				})
				.attr("class", function(d, i) {
					return a._svg_radar_g_axes_line_class + i;
				})
				.style("stroke", /*"#fff"*/ "balck")
				.style("stroke-width", "1px");

			a.radar_axes_text = a.radar_axes_g.selectAll(".axis")
				.data(a._allAxis)
				.enter()
				.append("text")
				.attr("class", function(d, i) {
					return a._svg_radar_g_axes_text_class;
				})
				.text(function(d) {
					return d
				})
				.style("font-family", "sans-serif")
				.style("font-size", "11px")
				.attr("text-anchor", "middle")
				.attr("dy", "1vw")
//				.attr('fill', '#ddd')
				.attr("transform", function(d, i) {
					return "translate(0, -10)"
				})
				.attr("x", function(d, i) {
					return a._levelFactor * (1 - a._factorLegend * Math.sin(i * a._radians / a._total)) - 60 * Math.sin(i * a._radians / a._total) + a._ExtraWidthX / 2;
				})
				.attr("y", function(d, i) {
					return a._levelFactor * (1 - Math.cos(i * a._radians / a._total)) - 20 * Math.cos(i * a._radians / a._total) + a._ExtraWidthY / 2;
				});
			//  ========== 
			//  = area = 
			//  ========== 
			series = 0;
			a._data.data.forEach(function(y, i) {
				y = y.value;
				dataValues = [];
				a.radar_node_g.selectAll(".nodes")
					.data(y, function(j, i) {
						j[1] != 0 ?
							dataValues.push([
								a._levelFactor * (1 - (parseFloat(Math.max(j[1], 0)) / a._maxValue) * a._factor * Math.sin(i * a._radians / a._total)) + a._ExtraWidthX / 2,
								a._levelFactor * (1 - (parseFloat(Math.max(j[1], 0)) / a._maxValue) * a._factor * Math.cos(i * a._radians / a._total)) + a._ExtraWidthY / 2
							]) : dataValues;
					});
				dataValues.push(dataValues[0]);
				a.radar_area_g.selectAll(".area")
					.data([dataValues])
					.enter()
					.append("polygon")
					.attr("class", a._svg_radar_g_area_class + series)
					.style("stroke-width", "2px")
					.style("stroke", a._colorScale(series))
					.attr("points", function(d) {
						var str = "";
						for(var pti = 0; pti < d.length; pti++) {
							str = str + d[pti][0] + "," + d[pti][1] + " ";
						}
						return str;
					})
					.style("fill", function(j, i) {
						return a._colorScale(series)
					})
					.style("fill-opacity", a._opacityArea)
					.on('mouseover', function(d) {
						z = "polygon." + d3.select(this).attr("class");
						a.radar_area_g.selectAll("polygon")
							.transition(200)
							.style("fill-opacity", 0.1);
						a.radar_area_g.selectAll(z)
							.transition(200)
							.style("fill-opacity", .7);
					})
					.on('mouseout', function() {
						a.radar_area_g.selectAll("polygon")
							.transition(200)
							.style("fill-opacity", a._opacityArea);
					});
				series++;
			});
			series = 0;

			a._data.data.forEach(function(y, x) {
				a.radar_node_g.selectAll(".nodes")
					.data(y.value).enter()
					.append("circle")
					.attr("class", a._svg_radar_g_node_class + series)
					.attr('r', a._node_radius)
					.attr("alt", function(j) { /*return Math.max(j[1], 0)*/
						return j[0]
					})
					.attr("cx", function(j, i) {
						j[1] != 0 ?
							dataValues.push([
								a._levelFactor * (1 - (parseFloat(Math.max(j[1], 0)) / a._maxValue) * a._factor * Math.sin(i * a._radians / a._total)) + a._ExtraWidthX / 2,
								a._levelFactor * (1 - (parseFloat(Math.max(j[1], 0)) / a._maxValue) * a._factor * Math.cos(i * a._radians / a._total)) + a._ExtraWidthY / 2
							]) : dataValues;
						return a._levelFactor * (1 - (Math.max(j[1], 0) / a._maxValue) * a._factor * Math.sin(i * a._radians / a._total)) + a._ExtraWidthX / 2;
					})
					.attr("cy", function(j, i) {
						return a._levelFactor * (1 - (Math.max(j[1], 0) / a._maxValue) * a._factor * Math.cos(i * a._radians / a._total)) + a._ExtraWidthY / 2;
					})
					.attr("data-id", function(j) {
						return j[0]
					})
					.style("fill", a._colorScale(series)).style("fill-opacity", .9)
					.on('mouseover', function(d) {
						newX = parseFloat(d3.select(this).attr('cx')) - 10;
						newY = parseFloat(d3.select(this).attr('cy')) - 5;

						a.radar_tooltip_class
							.attr('x', newX)
							.attr('y', newY)
							.text(d[1])
							.transition(200)
							.style('opacity', 1);

						z = "polygon." + d3.select(this).attr("class");
						a.radar_area_g.selectAll("polygon")
							.transition(200)
							.style("fill-opacity", 0.1);
						a.radar_node_g.selectAll(z)
							.transition(200)
							.style("fill-opacity", .7);
					})
					.on('mouseout', function() {
						a.radar_tooltip_class
							.transition(200)
							.style('opacity', 0);
						a.radar_area_g.selectAll("polygon")
							.transition(200)
							.style("fill-opacity", a._opacityArea);
					})
					.append("svg:title")
					.text(function(j) {
						return /*Math.max(j[1], 0)*/ j[0]
					});

				series++;
			});
			//Tooltip
			a.radar_tooltip_class
				.style('opacity', 0)
				.style('font-family', 'sans-serif')
				.style('font-size', '13px');
			//  ========== 
			//  = Title = 
			//  ========== 
			//Create the title for the legend
			a.legend_title_updata = a.legend_title_g.selectAll("rect").data([a._data.title]);
			a.legend_title_enter = a.legend_title_updata.enter();
			a.legend_title_exit = a.legend_title_updata.exit();
			a.legend_rect_updata = a.legend_rect_g.selectAll("rect").data(a._data.data);
			a.legend_rect_enter = a.legend_rect_updata.enter();
			a.legend_rect_exit = a.legend_rect_updata.exit();
			a.legend_text_updata = a.legend_text_g.selectAll("text").data(a._data.data);
			a.legend_text_enter = a.legend_text_updata.enter();
			a.legend_text_exit = a.legend_text_updata.exit();
			
			a.legend_title_updata
			.attr("class", a._svg_legend_g_title_class)
				.attr('transform', 'translate(0,0)')
				.attr("x", 0)
				.attr("y", 20)
				.attr("font-size", "10px")
//				.attr("fill", "#ddd")
				.text(a._data.title);
			a.legend_title_enter
			.append("text")
				.attr("class", a._svg_legend_g_title_class)
				.attr('transform', 'translate(0,0)')
				.attr("x", 0)
				.attr("y", 15)
				.attr("font-size", "10px")
//				.attr("fill", "#ddd")
				.text(a._data.title);
//			a.legend_title_exit.remove();
			
			
				//Create colour squares
				a.legend_rect_updata
				  .attr("x", 0)
				  .attr("y", function(d, i){ return (i+1) * 20 + 5;})
				  .attr("width", 20)
				  .attr("height", 20)
				  .style("fill", function(d, i){ return a._colorScale(i);})
				  ;
				  a.legend_rect_enter
				  .append("rect")
				  .attr("x", 0)
				  .attr("y", function(d, i){ return (i+1) * 20;})
				  .attr("width", 10)
				  .attr("height", 10)
				  .style("fill", function(d, i){ return a._colorScale(i);})
				  ;
				  a.legend_rect_exit.remove();
				  
				  a.legend_text_updata
				  .attr("x", 20)
				  .attr("y", function(d, i){ return (i+1) * 20 + 9;})
				  .attr("font-size", "10px")
				  .attr("fill", "#aaa")
				  .text(function(d) { return d.name; })
				  ;	
				  a.legend_text_enter
				  .append("text")
				  .attr("x", 20)
				  .attr("y", function(d, i){ return (i+1) * 20 + 9;})
				  .attr("font-size", "10px")
//				  .attr("fill", "#aaa")
				  .text(function(d) {console.log(d); return d.name; })
				  ;	
				  a.legend_text_exit.remove();
		}

		function Rounding(num) {
			//  ========== 
			//  = 此处不该纠结 = 
			//  ========== 
			var ii = 1;
			while(num >= 10) {
				num = (num - (num % 10)) / 10;
				ii++;
			}
			var c = Math.pow(10, ii)
			var o = Math.ceil(num / c)
			num = o * c;
			return num;
		}

		function CreateSvg_Radar(a) {

			a.svg = d3.select("#" + a._labelid)
				.append("svg")
				.attr("id", a._svgid)
				.attr("width",a._width)
				.attr("height",a._height)
//				.attr("preserveAspectRatio", "xMidYMid meet")
//				.attr("viewBox", "0 0 " + a._width + " " + a._height + " ");
			a.legendg = a.svg.append("g").attr("id", a._svg_legend_g_id);
			a.radarg = a.svg.append("g").attr("id", a._svg_radar_g_id)
				.attr("transform", "translate(" + a._TranslateX + "," + a._TranslateY + ")");

			a.legend_title_g = a.legendg.append("g").attr("id", a._svg_legend_g_title_g_id);
			a.legend_rect_g = a.legendg.append("g").attr("id", a._svg_legend_g_rect_g_id);
			a.legend_text_g = a.legendg.append("g").attr("id", a._svg_legend_g_text_g_id);

			a.radar_bgline_g = a.radarg.append("g").attr("id", a._svg_radar_g_bgline_g_id);
			a.radar_bgtext_g = a.radarg.append("g").attr("id", a._svg_radar_g_bgtext_g_id);

			a.radar_axes_g = a.radarg.append("g").attr("id", a._svg_radar_g_axes_g_id);

			a.radar_area_g = a.radarg.append("g").attr("id", a._svg_radar_g_area_g_id);
			a.radar_node_g = a.radarg.append("g").attr("id", a._svg_radar_g_node_g_id);

			a.radar_tooltip_g = a.radarg.append("g").attr("id", a._svg_radar_g_Tooltip_g_id);
			a.radar_tooltip_class = a.radar_tooltip_g.append("text").attr("class", a.radar_tooltip_class);
		}

		function Detect_function(object_lh) {
			return typeof(object_lh) == "undefined";
		}
	
 	
 	
     return radar; 
 }
 this.Radar = Radar;
}();